<?php
	include_once('./db/config.php');
	
	function getAllTopics()
	{
		$topics = false;
		try
		{
			connectDb("tictocwatches");
			global $dbLink;
			$queryString = "call sp_getAllTopics()";
			$resultPointer = mysqli_query($dbLink,$queryString);
			
			if($resultPointer)
			{
				$topics = array();
				while($resultRow = mysqli_fetch_assoc($resultPointer))
				{
					array_push($topics, $resultRow);
				}	
			}
			else
			{
				$queryError = mysqli_errno($dbLink);
				if($queryError!=0)
				{
					print_r($queryError);
				}
			}
		}
		catch(Exception $ex)
		{
			throw new Exception($ex->getMessage());
		}	
		return $topics;		
	}
	
	function createTopic($topicName, $topicDescription, $username)
	{	
		$result = false;
		try
		{
			connectDb("tictocwatches");
			global $dbLink;
			$queryString = "call sp_createTopic('".mysqli_real_escape_string($dbLink,$topicName)."','".mysqli_real_escape_string($dbLink,$topicDescription)."','".mysqli_real_escape_string($dbLink,$username)."')";
			$resultPointer = mysqli_query($dbLink,$queryString);
			
			if($resultPointer)
			{
				while($resultRow = mysqli_fetch_assoc($resultPointer))
				{
					$result = $resultRow["topic_id"];
				}
				return $result;				
			}
			else
			{
				$queryError = mysqli_errno($dbLink);
				if($queryError!=0)
				{
					print_r($queryError);
				}
			}
		}
		catch(Exception $ex)
		{
			throw new Exception($ex->getMessage());
		}	
		return $result;
	}

	function removeTopic($topicID)
	{	
		$result = false;
		try
		{
			connectDb("tictocwatches");
			global $dbLink;
			$queryString = "call sp_removeTopic(".mysqli_real_escape_string($dbLink,$topicID).")";
			$resultPointer = mysqli_query($dbLink,$queryString);
			
			if($resultPointer)
			{
				while($resultRow = mysqli_fetch_assoc($resultPointer))
				{
					$result = $resultRow["result"];
				}
				return $result;				
			}
			else
			{
				$queryError = mysqli_errno($dbLink);
				if($queryError!=0)
				{
					print_r($queryError);
				}
			}
		}
		catch(Exception $ex)
		{
			throw new Exception($ex->getMessage());
		}	
		return $result;
	}
	
	function getTopic($topicID)
	{	
		$result = false;
		try
		{
			connectDb("tictocwatches");
			global $dbLink;
			$queryString = "call sp_getTopic(".mysqli_real_escape_string($dbLink,$topicID).")";

			$resultPointer = mysqli_query($dbLink,$queryString);
			
			if($resultPointer)
			{
				while($resultRow = mysqli_fetch_assoc($resultPointer))
				{
					$result = $resultRow;
				}
				return $result;				
			}
			else
			{
				$queryError = mysqli_errno($dbLink);
				if($queryError!=0)
				{
					print_r($queryError);
				}
			}
		}
		catch(Exception $ex)
		{
			throw new Exception($ex->getMessage());
		}	
		return $result;
	}
	
	function getTopicPosts($topicID)
	{	
		$result = false;
		try
		{
			connectDb("tictocwatches");
			global $dbLink;
			$queryString = "call sp_getTopicPosts(".mysqli_real_escape_string($dbLink,$topicID).")";

			$resultPointer = mysqli_query($dbLink,$queryString);
			
			if($resultPointer)
			{
				$result = array();
				while($resultRow = mysqli_fetch_assoc($resultPointer))
				{
					array_push($result, $resultRow);
				}
				return $result;				
			}
			else
			{
				$queryError = mysqli_errno($dbLink);
				if($queryError!=0)
				{
					print_r($queryError);
				}
			}
		}
		catch(Exception $ex)
		{
			throw new Exception($ex->getMessage());
		}	
		return $result;
	}
	
	function updateTopic($topicID, $topicName, $topicDescription)
	{	
		$result = false;
		try
		{
			connectDb("tictocwatches");
			global $dbLink;
			$queryString = "call sp_updateTopic(".mysqli_real_escape_string($dbLink,$topicID).",'".mysqli_real_escape_string($dbLink,$topicName)."','".mysqli_real_escape_string($dbLink,$topicDescription)."')";
			
			$resultPointer = mysqli_query($dbLink,$queryString);
			
			if($resultPointer)
			{
				while($resultRow = mysqli_fetch_assoc($resultPointer))
				{
					$result = $resultRow["result"];
				}
				return $result;				
			}
			else
			{
				$queryError = mysqli_errno($dbLink);
				if($queryError!=0)
				{
					print_r($queryError);
				}
			}
		}
		catch(Exception $ex)
		{
			throw new Exception($ex->getMessage());
		}	
		return $result;
	}
?>
